#/bin/python
#coding:utf8
'''
Created on 2011-5-2

@author: mark
'''

import config

mc = config.memcache_client
db = config.database

def createchoice(_pid, _desc, _photo, _order):
    cid = db.insert('choice', pid=_pid, description=_desc, photo=_photo, rank=_order)
    return cid
def modifychoice(pid, _desc, _photo, _order):
    db.update("choice", where="pid=%s and rank=%s " % (pid, _order), description=_desc, photo=_photo)

def getchoicebyid(cid):
    key = str("choice_%s" % cid)
    choice = mc.get(key)
    if not choice:
        choice = _getchoicebyid(cid)
        if choice:
            mc.set(key, choice, config.cache_timeout)
    return choice

def _getchoicebyid(cid):
    pass

def getchoicebypid(pid):
    result = db.select('choice', where="pid = %s" % pid, order="rank asc")
    if result:
        return result[0], result[1]
    else:
        return None, None
    
def addCountByCid(cid):
    res = db.query("UPDATE choice set count = count +1 where cid = %s" % cid)
    return res == 1
